// pages/publish/pay/publishPayTask.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')
const systemInfo = wx.getWindowInfo();
const bottomInset = Math.abs(systemInfo.safeArea.bottom - systemInfo.windowHeight);
var price = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payPrice: '0',
    balance: '0',
    radioDikou: '1',
    taskNo: '',
    zhifuPrice: 0,
    addDays: 0,
    addCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    price = options.price.substring(1);
    this.setData({
      scrollViewBottomInset: bottomInset + 44,
      bottomInset: bottomInset,
      payPrice: price,
      taskNo: options.taskNo,
      addCount: options.addCount,
      addDays: options.addDays
    })
    this.requestAmount();
  },
  onChangeDikou(event) {
    if (event.detail == '1') {
      this.setData({
        radioDikou: event.detail,
        zhifuPrice: price
      });
    } else {
      if (parseFloat(this.data.balance) > 0) {
        if (parseFloat(this.data.balance) > parseFloat(price)) {
          this.setData({
            radioDikou: event.detail,
            zhifuPrice: 0
          });
        } else {
          this.setData({
            radioDikou: event.detail,
            zhifuPrice: parseFloat(price) - parseFloat(this.data.balance)
          });
        }
      } else {
        this.setData({
          radioDikou: event.detail,
          zhifuPrice: price
        });
      }
    }
  },
  //获取用户余额
  requestAmount() {
    let obj = {
      method: 'GET',
      url: '/user/' + wx.getStorageSync('USERDATA').platformId + '/account',
      data: {}
    }
    httpUtils.request(obj).then(res => {
      this.setData({
        balance: res.data.availableAmount,
      })
      if (parseFloat(res.data.availableAmount) > 0) {
        if (parseFloat(res.data.availableAmount) > parseFloat(price)) {
          this.setData({
            radioDikou: '2',
            zhifuPrice: 0
          });
        } else {
          this.setData({
            radioDikou: '2',
            zhifuPrice: parseFloat(price) - parseFloat(res.data.availableAmount)
          });
        }
      } else {
        this.setData({
          radioDikou: '1',
          zhifuPrice: price
        });
      }
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //请求支付
  actionPay() {
    //判断是否是增加任务份额
    if (this.data.addDays != null) {
      //不抵扣支付
      //直接走余额支付, 抵扣支付
      if (parseFloat(this.data.zhifuPrice) == 0 && this.data.radioDikou == '2') {
        let obj = {
          method: 'POST',
          contentType: 'application/x-www-form-urlencoded',
          url: '/task/renew/' + this.data.taskNo + '/balance',
          showLoading: true,
          data: {
            deductAmount: this.data.payPrice,
            taskIncreaseDays: this.data.addDays,
            increaseCount: this.data.addCount
          }
        }
        httpUtils.request(obj).then(res => {
          wx.navigateTo({
            url: '/pages/publish/pay/publishPaySuccess',
          })
        }).catch(err => {
          console.log('ERROR')
        });
      } else {
        wx.login({
          success: (res) => {
            var deductAmount = 0;
            if (this.data.radioDikou == '2') {
              deductAmount = this.data.balance;
            }
            let obj = {
              method: 'POST',
              contentType: 'application/x-www-form-urlencoded',
              url: '/task/renew/wechat/' + this.data.taskNo,
              showLoading: true,
              data: {
                deductAmount: deductAmount,
                oauthCode: res.code,
                taskIncreaseDays: this.data.addDays,
                increaseCount: this.data.addCount
              }
            }
            httpUtils.request(obj).then(res => {
              wx.requestPayment({
                nonceStr: res.data.weixinPayResultDto.noncestr,
                package: res.data.weixinPayResultDto.packageValue,
                paySign: res.data.weixinPayResultDto.paySign,
                timeStamp: res.data.weixinPayResultDto.timestamp,
                signType: res.data.weixinPayResultDto.signType,
                success(res) {
                  wx.navigateTo({
                    url: '/pages/publish/pay/publishPaySuccess',
                  })
                },
                fail(res) {
                  ui.showToast(res.errMsg);
                }
              })
            }).catch(err => {
              console.log('ERROR')
            });
          },
        })

      }
    } else {
      //不抵扣支付
      //直接走余额支付, 抵扣支付
      if (parseFloat(this.data.zhifuPrice) == 0 && this.data.radioDikou == '2') {
        let obj = {
          method: 'POST',
          contentType: 'application/x-www-form-urlencoded',
          url: '/payment/' + this.data.taskNo + '/balance',
          showLoading: true,
          data: {
            deductAmount: this.data.payPrice
          }
        }
        httpUtils.request(obj).then(res => {
          wx.navigateTo({
            url: '/pages/publish/pay/publishPaySuccess',
          })
        }).catch(err => {
          console.log('ERROR')
        });
      } else {
        wx.login({
          success: (res) => {
            var deductAmount = 0;
            if (this.data.radioDikou == '2') {
              deductAmount = this.data.balance;
            }
            let obj = {
              method: 'POST',
              contentType: 'application/x-www-form-urlencoded',
              url: '/payment/wechat/xcx/' + this.data.taskNo,
              showLoading: true,
              data: {
                deductAmount: deductAmount,
                oauthCode: res.code
              }
            }
            httpUtils.request(obj).then(res => {
              wx.requestPayment({
                nonceStr: res.data.noncestr,
                package: res.data.packageValue,
                paySign: res.data.paySign,
                timeStamp: res.data.timestamp,
                signType: res.data.signType,
                success(res) {
                  wx.navigateTo({
                    url: '/pages/publish/pay/publishPaySuccess',
                  })
                },
                fail(res) {
                  ui.showToast(res.errMsg);
                }
              })
            }).catch(err => {
              console.log('ERROR')
            });
          },
        })

      }
    }


  }
})